package io.renren.modules.wms.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.utils.excel.EasyExcelUtil;
import io.renren.common.utils.excel.ResponseMsg;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.wms.entity.WmsDataEntity;
import io.renren.modules.wms.service.WmsDataService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("wms")
public class WmsDataController {

    @Resource
    private WmsDataService wmsDataService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wmsDataService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        WmsDataEntity wmsData = wmsDataService.getById(id);

        return R.ok().put("wmsData", wmsData);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WmsDataEntity wmsData) {
        wmsData.setUpdateTime(new Date());
        wmsDataService.save(wmsData);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WmsDataEntity wmsData) {
        wmsData.setUpdateTime(new Date());
        wmsDataService.updateById(wmsData);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        wmsDataService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 删除全部
     */
    @RequestMapping("/deleteAll")
    public R deleteAll() {
        wmsDataService.deleteAll();
        return R.ok();
    }

    @ApiOperation(value = "下载模板", notes = "下载模板")
    @PostMapping("/downloadTemplate")
    public void downloadImportTemplate(HttpServletResponse response, HttpServletRequest request) {
        EasyExcelUtil.downloadImportTemplate(response, request, "1.xlsx");
    }

    @ApiOperation(value = "导入数据")
    @PostMapping("/importExcel")
    public ResponseMsg importExcel(@RequestParam("file") MultipartFile file, int type) {
        return wmsDataService.importExcel(file, type);
    }

    @ApiOperation(value = "抓取数据")
    @PostMapping("/capture")
    public ResponseMsg capture() {
        return wmsDataService.capture();
    }

    @ApiOperation(value = "比对数据")
    @PostMapping("/compare")
    public ResponseMsg compare() {
        return wmsDataService.compare();
    }

}
